package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.DietPlanItem;
import com.example.diethealthsystem.service.DietPlanItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/dietplanitems")
public class DietPlanItemController {

    private final DietPlanItemService dietPlanItemService;

    @Autowired
    public DietPlanItemController(DietPlanItemService dietPlanItemService) {
        this.dietPlanItemService = dietPlanItemService;
    }

    @GetMapping
    public ResponseEntity<List<DietPlanItem>> getAllDietPlanItems() {
        List<DietPlanItem> dietPlanItems = dietPlanItemService.findAll();
        return new ResponseEntity<>(dietPlanItems, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<DietPlanItem> getDietPlanItemById(@PathVariable Long id) {
        return dietPlanItemService.findById(id)
                .map(dietPlanItem -> new ResponseEntity<>(dietPlanItem, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<DietPlanItem> createDietPlanItem(@RequestBody DietPlanItem dietPlanItem) {
        DietPlanItem createdDietPlanItem = dietPlanItemService.save(dietPlanItem);
        return new ResponseEntity<>(createdDietPlanItem, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<DietPlanItem> updateDietPlanItem(@PathVariable Long id, @RequestBody DietPlanItem dietPlanItem) {
        return dietPlanItemService.findById(id)
                .map(existingDietPlanItem -> {
                    existingDietPlanItem.setDietPlan(dietPlanItem.getDietPlan());
                    existingDietPlanItem.setFood(dietPlanItem.getFood());
                    existingDietPlanItem.setPortion(dietPlanItem.getPortion());
                    existingDietPlanItem.setMealType(dietPlanItem.getMealType());
                    DietPlanItem updatedDietPlanItem = dietPlanItemService.save(existingDietPlanItem);
                    return new ResponseEntity<>(updatedDietPlanItem, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDietPlanItem(@PathVariable Long id) {
        if (dietPlanItemService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        dietPlanItemService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/dietplan/{dietPlanId}")
    public ResponseEntity<List<DietPlanItem>> getDietPlanItemsByDietPlanId(@PathVariable Long dietPlanId) {
        List<DietPlanItem> dietPlanItems = dietPlanItemService.findByDietPlanId(dietPlanId);
        if (dietPlanItems.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(dietPlanItems, HttpStatus.OK);
    }
} 